/*
 * @Author: 麦冬果果
 * @Date: 2021-09-07 11:18:17
 * @LastEditTime: 2021-09-09 16:54:10
 * @LastEditors: 麦冬果果
 * @Description: 快速接口
 * @FilePath: /svc/src/app/api/fastApi.go
 * 可以输入预定的版权声明、个性签名、空行等
 */

package api

import (
	"fastapi/src/app/dao"
	"fastapi/src/app/define"
	"fastapi/src/library/mhttp"

	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/util/gconv"
)

var FastApi = fastApi{}

type fastApi struct{}

func (a *fastApi) Index(r *ghttp.Request) {
	err := gconv.Bool(r.GetCtxVar("err"))
	if err {
		mhttp.Resp.FailJson(true, r, gconv.String(r.GetCtxVar("errMsg")))
	}
	apis := r.GetCtxVar("appApi")
	var appApi *define.AppApi
	gconv.Struct(apis, &appApi)

	if appApi.Method == "one" {
		a.one(r)
	}

	if appApi.Method == "all" {
		a.all(r)
	}

	if appApi.Method == "page" {
		a.page(r)
	}

	if appApi.Method == "add" {
		a.add(r)
	}

	if appApi.Method == "edit" {
		a.edit(r)
	}

	if appApi.Method == "delete" {
		a.delete(r)
	}

	mhttp.Resp.FailJson(true, r, "无方法信息")

}

//单个
func (a *fastApi) one(r *ghttp.Request) {
	apis := r.GetCtxVar("appApi")
	var appApi *define.AppApi
	gconv.Struct(apis, &appApi)
	req := gconv.MapDeep(r.GetCtxVar("request"))
	result, err := dao.FastApi.GetOne(appApi, req)
	if err != nil {
		g.Log().Error(err)
		mhttp.Resp.FailJson(true, r, "获取失败")
	}
	mhttp.Resp.SusJson(true, r, "获取单条数据成功", result)
}

//查询所有
func (a *fastApi) all(r *ghttp.Request) {
	apis := r.GetCtxVar("appApi")
	var appApi *define.AppApi
	gconv.Struct(apis, &appApi)

	req := gconv.MapDeep(r.GetCtxVar("request"))
	result, err := dao.FastApi.GetAll(appApi, req)
	if err != nil {
		g.Log().Error(err)
		mhttp.Resp.FailJson(true, r, "获取失败")
	}
	mhttp.Resp.SusJson(true, r, "获取所有数据成功", result)
}

//按页查询
func (a *fastApi) page(r *ghttp.Request) {
	apis := r.GetCtxVar("appApi")
	var appApi *define.AppApi
	gconv.Struct(apis, &appApi)

	req := gconv.MapDeep(r.GetCtxVar("request"))
	total, page, list, err := dao.FastApi.GetPage(appApi, req)
	if err != nil {
		g.Log().Error(err)
		mhttp.Resp.FailJson(true, r, "获取失败")
	}
	result := g.Map{
		"currentPage": page,
		"total":       total,
		"list":        list,
	}
	mhttp.Resp.SusJson(true, r, "获取列表数据成功", result)
}

// 新增
func (a *fastApi) add(r *ghttp.Request) {
	apis := r.GetCtxVar("appApi")
	var appApi *define.AppApi
	gconv.Struct(apis, &appApi)
	req := gconv.MapDeep(r.GetCtxVar("request"))
	result, err := dao.FastApi.Add(appApi, req)
	if err != nil {
		g.Log().Error(err)
		mhttp.Resp.FailJson(true, r, "新增失败")
	}
	var id int64
	if result != nil {
		id, _ = result.LastInsertId()
	}
	mhttp.Resp.SusJson(true, r, "添加成功", id)
}

// 修改
func (a *fastApi) edit(r *ghttp.Request) {
	apis := r.GetCtxVar("appApi")
	var appApi *define.AppApi
	gconv.Struct(apis, &appApi)
	req := gconv.MapDeep(r.GetCtxVar("request"))
	err := dao.FastApi.Edit(appApi, req)
	if err != nil {
		g.Log().Error(err)
		mhttp.Resp.FailJson(true, r, "修改失败")
	}
	mhttp.Resp.SusJson(true, r, "修改参数成功")
}

// 删除
func (a *fastApi) delete(r *ghttp.Request) {
	apis := r.GetCtxVar("appApi")
	var appApi *define.AppApi
	gconv.Struct(apis, &appApi)

	req := gconv.MapDeep(r.GetCtxVar("request"))
	err := dao.FastApi.Delete(appApi, req)
	if err != nil {
		g.Log().Error(err)
		mhttp.Resp.FailJson(true, r, "修改失败")
	}
	mhttp.Resp.SusJson(true, r, "删除成功")
}
